<?php

/**
 * @copyright : ienter asia Technologies
 * @author :  Hunglv
 * @version    1.0
 * @Description :  Time card manipulation allow show data get form file csv and save data to table timecard
 */
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Mod_tcmanipulation extends MYAD_Controller {
    /**
     * function __construct
     * not overloading parent __construct
     */    
    function __construct() {
        parent::__construct();
        $this->load->model('mod_shiftinfo/Mmod_shiftinfo');
        $this->load->model('mod_overtime/Mmod_overtime');
        $this->load->model('mod_leaveregistration/Mmod_leaveregistration');
        $this->load->model('mod_employee/Mmod_employee');
        $this->load->model('Mmod_tcmanipulation');
        session_start();
    }
    /**
     * function index
     * show data load from session
     */      
    function index(){
        $data['title'] = $this->lang->line('tcmanipulation_title');
        $data['ReviewData'] = $this->lang->line('tcmanipulation_ReviewData');
        $data['SelectData'] = $this->lang->line('tcmanipulation_SelectData');
        $data['show'] = "index";        
        if(isset($_SESSION['arrCard'])&& !empty($_SESSION['arrCard'])){
            $data['data'] = $_SESSION['arrCard'];
        }
        else{
            redirect('/upload');
        }
        $this->load->view('layout', $data);        
    }
    /**
     * function import
     * data processing and save table timecard 
     */     
    function import(){
        // set current date
        $maxDates = '';
        // set array temp empty data
        $temp = array();
        // check submit
        if($this->input->post('btnSubmit')){
            // check exist session
           if(isset($_SESSION['arrCard'])&& !empty($_SESSION['arrCard'])){
               // order array input
                $arrTime = (isset($_SESSION['arrCard'])) ? $_SESSION['arrCard'] : '';
                //$arrTime = $this->Mmod_tcmanipulation->array_orderby($arrTime, '1', SORT_ASC,'3', SORT_ASC);
                // end order array input
                // check option value
                if (isset($_POST['cboEmployeeID'])&& $_POST['cboEmployeeID']==1 && isset($_POST['cboDate'])&& $_POST['cboDate']==3 && isset($_POST['cboArrivaltime'])&&$_POST['cboArrivaltime']==4&& isset($_POST['cboLeavetime'])&& $_POST['cboLeavetime']==5){
                    /*
                     * $ColemployeeID = 1
                     * $Coldate = 3
                     * $Colarrivaltime = 4
                     * $Colleavetime = 5
                     */
                    $ColemployeeID      = $_POST['cboEmployeeID'];
                    $Coldate            = $_POST['cboDate'];
                    $Colarrivaltime     = $_POST['cboArrivaltime'];
                    $Colleavetime       = $_POST['cboLeavetime'];
                    // get shift info
                    $shiftInfoBase = $this->Mmod_tcmanipulation->listShiftInfo();
                    // get employee info
                    $employeeInfo = $this->Mmod_tcmanipulation->listAllEmployee();
                    // check exist little one employee
                    if(count($employeeInfo)>0){
                        // check exist little one employee
                        if(count($shiftInfoBase)>0){
                           // get curent month and year 
                            if(isset($arrTime[0]) && count($arrTime[0])>0){
                                $maxDates = $arrTime[0][$Coldate];
                            }
                            else{
                                if(isset($arrTime[1]) && count($arrTime[1])>0){
                                    $maxDates = $arrTime[1][$Coldate];
                                }
                            }
                           $flag = $this->Mmod_tcmanipulation->allTimeWorkingForEmployeeFromData($shiftInfoBase,$employeeInfo,$arrTime,$ColemployeeID,$Coldate,$Colarrivaltime,$Colleavetime);
                           if($flag == TRUE){
                                $tempData = array(
                                    'cmbMonth' => date('m', strtotime($maxDates)),
                                    'cmbYear' => date('Y', strtotime($maxDates)),
                                );
                                $this->session->set_userdata($tempData);
                                // clear all session
                                unset($_SESSION['arrCard']);
                                // Go to page listtimecard
                                redirect('/listtimecard');
                           } 
                           else{
                               // set message show error
                                $temp['message'] = $this->lang->line('tcmanipulation_updateDB_fall');
                                // set flag
                                $temp['flag'] = FALSE;        
                                if(isset($_SESSION['arrCard'])&& !empty($_SESSION['arrCard'])){
                                    $temp['data'] = $_SESSION['arrCard'];
                                }
                                else{
                                    redirect('/upload');
                                }                      
                           }
                        }
                        else{
                            $temp['flag'] = FALSE;
                            $temp['message'] = $this->lang->line('tcmanipulation_configshiftinfo');  
                            $temp['url'] = 'shift-info';  
                            if(isset($_SESSION['arrCard'])&& !empty($_SESSION['arrCard'])){
                                $temp['data'] = $_SESSION['arrCard'];
                            }
                            else{
                                redirect('/upload');
                            }
                        }
                    }
                    else{
                        $temp['flag'] = FALSE;
                        $temp['message'] = $this->lang->line('tcmanipulation_add_employee');
                        $temp['url'] = 'employee'; 
                        if(isset($_SESSION['arrCard'])&& !empty($_SESSION['arrCard'])){
                            $temp['data'] = $_SESSION['arrCard'];
                        }
                        else{
                            redirect('/upload');
                        }                        
                    }
                }
                else{
                    $this->session->set_flashdata('message', $this->lang->line('tcmanipulation_Chosedata'));        
                    if(isset($_SESSION['arrCard'])&& !empty($_SESSION['arrCard'])){
                        $temp['data'] = $_SESSION['arrCard'];
                    }
                    else{
                        redirect('/upload');
                    }                  
                }
           }
            else{
                    redirect('/upload');                    
            }           
        }
        else{      
            if(isset($_SESSION['arrCard'])&& !empty($_SESSION['arrCard'])){
                $temp['data'] = $_SESSION['arrCard'];
            }
            else{
                redirect('/upload');
            }                
        }
        $info = $this->Mmod_tcmanipulation->getInfo();
        $data = array_merge($temp,$info);
        $this->load->view('layout', $data); 
    }
    
}
?>